package com.ruijie.acl.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.seam.annotations.In;

import com.ruijie.acl.importdevice.service.ImportAsyncServiceParameter;
import com.ruijie.emp.common.BaseAsynLogAction2;
import com.ruijie.emp.components.deviceselector.service.DeviceSelectorService;
import com.ruijie.emp.components.deviceselector.web.DeviceSelector;
import com.ruijie.emp.device.Device;
import com.ruijie.emp.system.role.type.PurviewEnum;
import com.ruijie.emp.system.service.DeviceModelService;

public abstract class BaseImportDeviceAsynLogAction extends BaseAsynLogAction2 {
	Map<Long, Boolean> selectIdsMap = null;
	/* 设备选择器  */
	private DeviceSelector deviceSelector;

	
    @In("#{deviceSelectorService}")
    private DeviceSelectorService deviceSelectorService;

    @In("#{deviceModelService}")
    private DeviceModelService deviceModelService;

    public DeviceSelector getDeviceSelector() {
        if (null == deviceSelector)
            initDeviceSelector();

        return deviceSelector;
    }
    
    /**
     * 初始化设备选择器
     */
    public void initDeviceSelector() {       
        deviceSelector = new DeviceSelector(deviceSelectorService, deviceModelService,PurviewEnum.WRITE.toString());  
    }

	/**
	 * 取得CE设备的id
	 * 
	 * @return id列表
	 */
	public Long[] getIds() {
		selectIdsMap = deviceSelector.getSelectedSelector().getSelectedIds();
		if (selectIdsMap != null && selectIdsMap.size() > 0){
			List<Long> ids = new ArrayList<Long>();
	        Set<Long> ketSet = selectIdsMap.keySet();
	        for (Long key : ketSet) {
	            if (selectIdsMap.get(key).equals(Boolean.TRUE)) {
	            	ids.add(key);
	            }
	        }
	        if (ids.size() > 0) {
	            Long[] idArr = new Long[ids.size()];
	            int i = 0;
	            for (Long id : ids) {
	                idArr[i] = id;
	                i++;
	            }
	            return idArr;
	        }
		}
		return new Long[0];
    }
	@Override
	public Integer getCurrentPercentage() {
		if(currentStatusMessage==null){
			return 0 ;
		}		
		long total=currentStatusMessage.getTotal();
		long success=currentStatusMessage.getCountOfSuccess();
		long fail=currentStatusMessage.getCountOfFailure();
		if(total>0){
			long percentage=(success+fail)*100/total;
			return (int)percentage; 
		}else{
			return 0;
		}
	}
	/**
	 * 取得CE设备的id
	 * 
	 * @return id列表
	 */
	public Long[] getAllIds() {
		selectIdsMap = deviceSelector.getSelectedSelector().getSelectedIds();
		if (selectIdsMap != null && selectIdsMap.size() > 0){
			List<Long> ids = new ArrayList<Long>();
	        Set<Long> ketSet = selectIdsMap.keySet();
	        for (Long key : ketSet) {
	            //if (selectIdsMap.get(key).equals(Boolean.TRUE)) {
	            	ids.add(key);
	            //}
	        }
	        if (ids.size() > 0) {
	            Long[] idArr = new Long[ids.size()];
	            int i = 0;
	            for (Long id : ids) {
	                idArr[i] = id;
	                i++;
	            }
	            return idArr;
	        }
		}
		return new Long[0];
    }
	
	@Override
	public String startService() {
		List<Device>	devices=deviceSelector.getAllSelectedDevices();
//		Long[] ids = this.getAllIds();
//		if (ids == null || ids.length == 0){
//			return null;
//		}	
		 if (devices.size() > 0) {
	            Long[] idArr = new Long[devices.size()];
	            int i = 0;
	            for(Device device:devices){
	            	 idArr[i] = device.getId();
		                i++;
	    		}
	            
	        	ImportAsyncServiceParameter parameter = new ImportAsyncServiceParameter();
	    		parameter.setDeviceIds(idArr);
	    		getServer().start(parameter);
	        }

		return getLogPage();
	}
}
